<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="字段" prop="field">
        <el-select v-model="queryParams.field" placeholder="请选择" clearable>
          <el-option
            v-for="dict in fieldList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="条件" prop="exp">
        <el-select v-model="queryParams.exp" placeholder="请选择" clearable>
          <el-option
            v-for="dict in sqlExps"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="值" prop="val">
        <el-input
          v-model="queryParams.val"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column
        :label="f.name"
        align="center"
        :prop="f.code"
        v-for="(f, i) in fieldList"
        :key="i"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  apiGet,
  apiPost,
  apiPut,
  apiDelete,
  apiDs,
  apiExport,
  apiDownload,
} from "@/api/openapi";
export default {
  name: "Field",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      modelId: null,
      // 表格数据
      dataList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        field: null,
        exp: "eq",
        val: null,
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      sqlExps: [
        { value: "eq", label: "等于" },
        { value: "neq", label: "不等于" },
        { value: "lk", label: "包含" },
        { value: "gt", label: "大于" },
        { value: "lt", label: "小于" },
      ],
      // 表单参数
      form: {},
      modelInfo: null,
      fieldList: [],
    };
  },
  created() {
    const modelId = this.$route.params && this.$route.params.modelId;
    this.modelId = modelId;
    this.queryParams.model_id = modelId;
    let params = {
      api_model: {
        id: modelId,
      },
      api_field: {
        model_id: modelId,
        "@pager": { limit: 100, order: "id ASC" },
      },
    };
    apiGet(params).then((response) => {
      this.modelInfo = response.data["api_model"];
      this.fieldList = response.data["api_field"];

      this.getList();
    });
  },
  methods: {
    /** 查询模型字段列表 */
    getList() {
      this.loading = true;
      let params = { "@pager": this.pager, "@table": this.modelInfo["code"] };
      if (this.queryParams.field) {
        params["@exp"] = {};
        params["@exp"][this.queryParams.field] = this.queryParams.exp;
        params[this.queryParams.field] = this.queryParams.val;
      }
      apiGet(params).then((response) => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
